<?php

namespace app\admin\model;

use think\Session;

class User extends BaseModel
{
    protected $table='jc_user';
    //用户列表
     public function viplist()
    {
        $uid = Session::get('admin_uid');
        $vip = Guanli::where('id',$uid)->find();
        $arr = User::where('uid',$vip['uid'])->find();
        $rs=$this
            ->alias('us')
            ->where('us.uid','neq','1')
            ->order('us.uid desc')
            ->paginate(7);
        if($vip['gid']!=1)
        {
            if (input('sub')) {
                $cateid = input('cateid', '');//下拉菜单
                $key = input('Key');//输入框
                $datemin = input('datemin', '');//最小时间
                $datemax = input('datemax', '');//最大的吧
                //判断$key是不是空
                //判断时间
                if (isset($datemin) && $datemin != '') {
                    $rs = $rs->whereTime('jc_user.u_createtime', '>=', input('datemin') . ' 00:00:00');
                    //print_r($rs);die;
                }
                if (isset($datemax) && $datemax != '') {
                    $rs = $rs->whereTime('jc_user.u_createtime', '<=', input('datemax') . ' 23:59:59');
                }
                if ($cateid == '') {
                    //$rs=$rs->paginate(7,false,['query'=>request()->param()]);
                    foreach ($rs as $k => $v) {
                        //查找下线个数
                        $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                        $rs[$k]['u_path'] = $count;
                    }
                    return $rs;
                }
                if ($key == '') {
                    //$rs=$rs->paginate(7,false,['query'=>request()->param()]);
                    foreach ($rs as $k => $v) {
                        //查找下线个数
                        $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                        $rs[$k]['u_path'] = $count;
                    }
                    return $rs;
                }
                $rs = $this
                    ->alias('us')
                    ->where('us.u_path', 'like', '%,' . $arr['uid'] . ',%')
                    ->where($cateid, 'like', '%' . $key . '%')
                    ->order('us.uid desc')
                    ->paginate(7);
                foreach ($rs as $k => $v) {
                    //查找下线个数
                    $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                    $rs[$k]['u_path'] = $count;
                }
                return $rs;
            }
            $rs = $this
                ->alias('us')
                ->where('us.u_path', 'like', '%,' . $arr['uid'] . ',%')
                ->order('us.uid desc')
                ->paginate(7);
            foreach ($rs as $k => $v) {
                //查找下线个数
                $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                $rs[$k]['u_path'] = $count;
            }
            return $rs;
        }else {
            if (input('sub')) {
                $cateid = input('cateid', '');//下拉菜单
                $key = input('Key');//输入框
                $datemin = input('datemin', '');//最小时间
                $datemax = input('datemax', '');//最大的吧
                //判断$key是不是空
                //判断时间
                if (isset($datemin) && $datemin != '') {
                    $rs = $rs->whereTime('jc_user.u_createtime', '>=', input('datemin') . ' 00:00:00');
                    //print_r($rs);die;
                }
                if (isset($datemax) && $datemax != '') {
                    $rs = $rs->whereTime('jc_user.u_createtime', '<=', input('datemax') . ' 23:59:59');
                }
                if ($cateid == '') {
                    //$rs=$rs->paginate(7,false,['query'=>request()->param()]);
                    foreach ($rs as $k => $v) {
                        //查找下线个数
                        $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                        $rs[$k]['u_path'] = $count;
                    }
                    return $rs;
                }
                if ($key == '') {
                    //$rs=$rs->paginate(7,false,['query'=>request()->param()]);
                    foreach ($rs as $k => $v) {
                        //查找下线个数
                        $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                        $rs[$k]['u_path'] = $count;
                    }
                    return $rs;
                }
                $rs = $rs->where($cateid, 'like', '%' . $key . '%')->paginate(4);
                foreach ($rs as $k => $v) {
                    //查找下线个数
                    $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                    $rs[$k]['u_path'] = $count;
                }
                return $rs;
            } else {
                //$rs=$rs->paginate(7,false,['query'=>request()->param()]);
                foreach ($rs as $k => $v) {
                    //查找下线个数
                    $count = self::where('FIND_IN_SET(:u_path,u_path)', ['u_path' => $v['uid']])->count();
                    $rs[$k]['u_path'] = $count;
                }
                //dump($this->objectToarray($rs));die;
                return $rs;
            }
        }
    }
   //用户列表详情页面的查看
    public function chakan($uid)
    {
        $rs=$this->alias('us')->where('us.uid',$uid)->find();
        if($rs['pid']==0)
        {
            $rs['tuiname']='此账号未主账号';
            $rs['tuiphone']='此账号未主账号';
        }
        else
        {
            $name=$this->where('uid',$rs['pid'])->find();
            $rs['tuiname']= $name['u_name'];
            $rs['tuiphone']= $name['u_phone'];
        }
        return $rs;
        //print_r($this->objectToarray($rs));
    }

    //用户详情页的修改
    public function vipxiugai($uid,$data)
    {
        $rs=$this->where('uid',$uid)->update($data);
        if($rs!==false)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    //用户代理状态的改变
    public function daili()
    {
        $uid=input('uid');
        $rs=$this->where('uid',$uid)->find();
        if($rs['daili']==1)
        {
            $guanli=Guanli::where('uid',$rs['uid'])->delete();
            $daili=0;
        }
        else
        {
            //后台添加代理账号
            $gname=$rs['u_phone'];
            $pwd=md5($rs['u_pwd']);
            $guanli=Guanli::insert(['uid'=>$uid,'phone'=>$gname,'gname'=>$gname,'pwd'=>$pwd,'gid'=>2]);
            $daili=1;
        }
        $xiu=$this->where('uid',$uid)->update(['daili'=>$daili]);
        if($xiu)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    //用户锁定状态的改变
    public function suoding()
    {
        $uid=input('uid');
        $rs=$this->where('uid',$uid)->find();
        if($rs['u_state']==1)
        {
            $daili=0;
        }
        else
        {
            $daili=1;
        }
        $xiu=$this->where('uid',$uid)->update(['u_state'=>$daili]);
        if($xiu)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
  	//代理显示
    public static function getDailiSetup(){
        $key = input('key');
        $keyold = input('keyold');
        $gid = Session::get("admin_uid");
        $guanli = new Guanli();
        $guanli = $guanli->where('id',$gid)->find();
        $users = self::where('uid',$guanli['uid'])->find();
        $user = self::where('did',$users['uid'])->select();
        if (request()->isAjax()) {
            if ($users['did'] == 0) {
                foreach ($user as $v) {
                    $k[] = $v['uid'];
                }
                foreach ($k as $v) {
                    $d = self::where('uid', $v)->find();
                    if ($d['daili'] == 1) {
                        $dailis = self::where('uid', $d['uid'])->update(['lilv' => $key]);
                    } else {
                        $dailis = self::where('uid', $d['uid'])->update(['lilv' => $keyold]);
                    }
                }
                if ($dailis) {
                    return 1;
                } else {
                    return 0;
                }
            } else {
                foreach ($user as $v) {
                    $k[] = $v['uid'];
                }
                foreach ($k as $v) {
                    $dailis = self::where('uid', $v)->update(['lilv' => $key]);
                }
                if ($dailis) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
        $arr = self::where('uid',$guanli['uid'])->find();
        return $arr;
    }

}